{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "e687359d-d80c-44d2-b402-ee524c72575d",
   "metadata": {},
   "source": [
    "# L2: Deploying Segmentation Models On-Device\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3586246b-05e3-4fd8-8e60-95c243e16c19",
   "metadata": {},
   "source": [
    "<p style=\"background-color:#fff6e4; padding:15px; border-width:3px; border-color:#f5ecda; border-style:solid; border-radius:6px\"> ⏳ <b>Note <code>(Kernel Starting)</code>:</b> This notebook takes about 30 seconds to be ready to use. You may start and watch the video while you wait.</p>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fcf08825-0342-467e-924f-50a58060b8e0",
   "metadata": {},
   "source": [
    "[FFNet Paper](https://arxiv.org/abs/2206.08236)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0170b9e3-4f8b-42cd-9f48-082ae7cb4172",
   "metadata": {},
   "outputs": [],
   "source": [
    "from qai_hub_models.models.ffnet_40s import Model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "42d5d067-7207-4dcf-b892-ea09ff502a9f",
   "metadata": {},
   "outputs": [],
   "source": [
    "from torchinfo import summary"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c05e4f1d-9bba-4aa0-99a6-f614bb5bee8a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Load from pre-trained weights\n",
    "model = Model.from_pretrained()\n",
    "input_shape = (1, 3, 1024, 2048)\n",
    "stats = summary(model, \n",
    "  input_size=input_shape, \n",
    "  col_names=[\"num_params\", \"mult_adds\"]\n",
    ")\n",
    "print(stats)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "572da5ac-e63d-4bc9-b38d-1f82e7ea445b",
   "metadata": {},
   "source": [
    "## Exercise: Try another variant of FFNet"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "44331344-1485-4c22-bed2-dcd36e6d76c3",
   "metadata": {},
   "outputs": [],
   "source": [
    "# High resolution variants\n",
    "from qai_hub_models.models.ffnet_40s import Model\n",
    "#from qai_hub_models.models.ffnet_54s import Model\n",
    "#from qai_hub_models.models.ffnet_78s import Model\n",
    "\n",
    "# Low resolution variants\n",
    "low_res_input_shape = (1, 3, 512, 1024)\n",
    "#from qai_hub_models.models.ffnet_78s_lowres import Model\n",
    "#from qai_hub_models.models.ffnet_122ns_lowres import Model\n",
    "\n",
    "model = Model.from_pretrained()\n",
    "stats = summary(model, \n",
    "  input_size=input_shape, # use low_res_input_shape for low_res models\n",
    "  col_names=[\"num_params\", \"mult_adds\"]\n",
    ")\n",
    "print(stats)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "edc33688-5a9e-483f-a641-03c3ad9c2d1b",
   "metadata": {},
   "source": [
    "## Setup AI Hub for device-in-the-loop deployment"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3842bf22-3f02-48ba-b46f-cd68e61a81ce",
   "metadata": {},
   "outputs": [],
   "source": [
    "import qai_hub"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d9f38ad0-0221-4be7-a67e-f541ab84f033",
   "metadata": {},
   "source": [
    "<p style=\"background-color:#fff6ff; padding:15px; border-width:3px; border-color:#efe6ef; border-style:solid; border-radius:6px\"> 💻 &nbsp; <b>Access Utils File and Helper Functions:</b> To access the files for this notebook, 1) click on the <em>\"File\"</em> option on the top menu of the notebook and then 2) click on <em>\"Open\"</em>. For more help, please see the <em>\"Appendix - Tips and Help\"</em> Lesson.</p>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2a0f8c71-f386-4389-a6b1-1412e89186b6",
   "metadata": {},
   "outputs": [],
   "source": [
    "from utils import get_ai_hub_api_token\n",
    "ai_hub_api_token = get_ai_hub_api_token()\n",
    "\n",
    "!qai-hub configure --api_token $ai_hub_api_token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "661c2173-ede8-45ea-9da2-73be38add485",
   "metadata": {},
   "outputs": [],
   "source": [
    "%run -m qai_hub_models.models.ffnet_40s.demo"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aceeb0c5-9b12-4122-858a-3dfacee038d7",
   "metadata": {},
   "source": [
    "## Run on a real smart phone!"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "71d79543-006f-4e77-92d7-c773d6396402",
   "metadata": {},
   "source": [
    "<p style=\"background-color:#fff6e4; padding:15px; border-width:3px; border-color:#f5ecda; border-style:solid; border-radius:6px\"> ⏳ <b>Note:</b> To spread the load across various devices, we are selecting a random device. Feel free to change it to any other device you prefer.</p>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bee2a8cd-ba41-4ea9-93a1-4eaf493dcf02",
   "metadata": {},
   "outputs": [],
   "source": [
    "devices = [\n",
    "    \"Samsung Galaxy S22 Ultra 5G\",\n",
    "    \"Samsung Galaxy S22 5G\",\n",
    "    \"Samsung Galaxy S22+ 5G\",\n",
    "    \"Samsung Galaxy Tab S8\",\n",
    "    \"Xiaomi 12\",\n",
    "    \"Xiaomi 12 Pro\",\n",
    "    \"Samsung Galaxy S22 5G\",\n",
    "    \"Samsung Galaxy S23\",\n",
    "    \"Samsung Galaxy S23+\",\n",
    "    \"Samsung Galaxy S23 Ultra\",\n",
    "    \"Samsung Galaxy S24\",\n",
    "    \"Samsung Galaxy S24 Ultra\",\n",
    "    \"Samsung Galaxy S24+\",\n",
    "]\n",
    "\n",
    "import random\n",
    "selected_device = random.choice(devices)\n",
    "print(selected_device)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "434933a8-6993-459e-8127-8a39daa18645",
   "metadata": {},
   "outputs": [],
   "source": [
    "%run -m qai_hub_models.models.ffnet_40s.export -- --device \"$selected_device\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "84fa5848-0d48-43cd-b600-30ea8d31c2c9",
   "metadata": {},
   "source": [
    "<p style=\"background-color:#fff1d7; padding:15px; \"> <b>Note</b>: To view the URL for each job, you require login. You can experience sample results in the following urls.</p>\n",
    "\n",
    "* [FFNet 40s](https://aihub.qualcomm.com/mobile/models/ffnet_40s)\n",
    "* [FFNet 54s](https://aihub.qualcomm.com/mobile/models/ffnet_54s)\n",
    "* [FFNet 78s](https://aihub.qualcomm.com/mobile/models/ffnet_78s)\n",
    "* [FFNet 78s-low-res](https://aihub.qualcomm.com/mobile/models/ffnet_78s_lowres)\n",
    "* [FFNet 122ns-low-res](https://aihub.qualcomm.com/mobile/models/ffnet_122ns_lowres)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f92bccf1-ed3b-42fe-94a2-c35b0dcd4bdf",
   "metadata": {},
   "source": [
    "## On Device Demo"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "102603d1-1de1-43ea-8378-6df6e0220aec",
   "metadata": {},
   "outputs": [],
   "source": [
    "%run -m qai_hub_models.models.ffnet_40s.demo -- --device \"$selected_device\" --on-device"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c7e23659-79e5-44aa-9a67-88e50ec28b97",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "05484a9a-2de6-45c7-b78a-9951c764774d",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "95fd621a-e26c-4140-9ca9-9dd7185d31f5",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ad1020e4-4468-4a04-8a87-7a846d47fd7c",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "94d34187-75f1-4d27-888f-3f5246433d54",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.19"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
